package com.example.a12;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;

public class A12Application {

    interface Foo {

        void foo();

        int bar();

    }

    static class Target implements Foo {

        @Override
        public void foo() {
            System.out.println("target foo");
        }

        @Override
        public int bar() {
            System.out.println("target bar");
            return 100;
        }
    }

//    interface InvocationHandler {
//        Object invoke(Object proxy, Method method, Object[] args) throws Throwable;
//    }

    public static void main(String[] args) {
        Foo proxy0 = new $Proxy0(new InvocationHandler() {
            @Override
            public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                // 1. 功能增强
                System.out.println("before");
                // 2.调用目标
//                new Target().foo();
                return method.invoke(new Target(), args);

            }
        });
        proxy0.foo();
        proxy0.bar();

        /*
            小结: 代理一点都不难，无非是利用了多态，反射的知识
         */
    }
}
